package com.epam.edu.dao;

import com.epam.edu.constant.Role;
import com.epam.edu.entity.User;

import java.sql.*;

public class DAOUser {
    public static int getUserID(Connection connection, User user) {
        int result = -1;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            String command = "SELECT id,role FROM Users WHERE login=? AND password=? AND isDelete='false'";
            statement = connection.prepareStatement(command);
            statement.setString(1, user.getName());
            statement.setString(2, user.getPassword());
            resultSet = statement.executeQuery();
            if (resultSet.next()) {
                result = resultSet.getInt(1);
                user.setId(result);
                user.setRole(Role.valueOf(resultSet.getString(2)));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (statement != null) statement.close();
                if (resultSet != null) resultSet.close();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
        user.setId(result);
        return result;
    }

    public static int add(Connection connection, User user) {
        int id = -1;
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            String command = String.format("INSERT INTO Users (login,password,role) VALUES('%s','%s','%s')",
                    user.getName(), user.getPassword(), user.getRole().toString());
            statement = connection.createStatement();
            statement.executeUpdate(command, Statement.RETURN_GENERATED_KEYS);
            resultSet = statement.getGeneratedKeys();
            resultSet.next();
            id = resultSet.getInt(1);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (statement != null) statement.close();
                if (resultSet != null) resultSet.close();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
        return id;
    }

    public static boolean isUser(Connection connection, String userName) {
        boolean result = false;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            String command = "SELECT id FROM Users WHERE login=? AND isDelete='false'";
            statement = connection.prepareStatement(command);
            statement.setString(1, userName);
            resultSet = statement.executeQuery();
            result = resultSet.next();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (statement != null) statement.close();
                if (resultSet != null) resultSet.close();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
        return result;
    }

    public static void changePassword(Connection connection, int userId, String newPassword) {
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            String command = String.format("UPDATE Users SET password='%s' WHERE id=%s",
                    newPassword, userId);
            statement = connection.createStatement();
            statement.executeUpdate(command);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (statement != null) statement.close();
                if (resultSet != null) resultSet.close();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
    }

    public static boolean isPassword(Connection connection, int userId, String password) {
        boolean result = false;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            String command = "SELECT id FROM Users WHERE id=? AND password=? AND isDelete='false'";
            statement = connection.prepareStatement(command);
            statement.setInt(1, userId);
            statement.setString(2, password);
            resultSet = statement.executeQuery();
            result = resultSet.next();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (statement != null) statement.close();
                if (resultSet != null) resultSet.close();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
        return result;
    }

    public static void delete(Connection connection, int userId) {
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            String command = String.format("UPDATE Users SET isDelete='true' WHERE id=%s", userId);
            statement = connection.createStatement();
            statement.executeUpdate(command);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (statement != null) statement.close();
                if (resultSet != null) resultSet.close();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
    }

    public static void remove(Connection connection, int userId) {
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            String command = String.format("DELETE Users WHERE id=%s", userId);
            statement = connection.createStatement();
            statement.executeUpdate(command);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (statement != null) statement.close();
                if (resultSet != null) resultSet.close();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
    }

    public static void update(Connection connection, User user) {
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            String command = String.format("UPDATE Users " +
                    "SET login='%s', password='%s',role='%s' WHERE id=%s",
                    user.getName(), user.getPassword(), user.getRole().toString(), user.getId());
            statement = connection.createStatement();
            statement.executeUpdate(command);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (statement != null) statement.close();
                if (resultSet != null) resultSet.close();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
    }

    public static User selectById(Connection connection, int id) {
        User user = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            String command = "SELECT login, password,role FROM Users WHERE id=? AND isDelete='false'";
            statement = connection.prepareStatement(command);
            statement.setInt(1, id);
            resultSet = statement.executeQuery();
            if (resultSet.next()) {
                user = new User();
                user.setName(resultSet.getString(1));
                user.setPassword(resultSet.getString(2));
                user.setRole(Role.valueOf(resultSet.getString(3)));
                user.setId(id);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (statement != null) statement.close();
                if (resultSet != null) resultSet.close();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
        return user;
    }
}
